package com.wang.dao.impl;

import java.sql.ResultSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.wang.dao.IUserDao;
import com.wang.domain.User;
@Repository
public class UserDaoImpl implements IUserDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public void add(User user) {
		jdbcTemplate.update("insert into student (name,password) values (?,?)", user.getName(),user.getPassword());
	}

	@Override
	public void delete(Long id) {
		jdbcTemplate.update("delete from student where id =?",id);
		
	}

	@Override
	public void update(User user) {
		jdbcTemplate.update("update student set name=?,password=? where id=?",user.getName(),user.getPassword(),user.getId());
	}

	@Override
	public User queryOne(Long id) {
		/*List<User> query = jdbcTemplate.query("select * from student where id=?", (rs,num)->{
			User user = new User();
			user.setId(rs.getLong("id"));
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("password"));
			return user;
		}, id);
		return query.get(0);*/
		return jdbcTemplate.queryForObject("select * from student where id=?", new BeanPropertyRowMapper<User>(User.class), id);
	}

	@Override
	public List<User> queryAll() {
		/*return jdbcTemplate.query("select * from student", (rs,num)->{
			User user = new User();
			user.setId(rs.getLong("id"));
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("password"));
			return user;
		});*/
		return jdbcTemplate.query("select * from student", new BeanPropertyRowMapper<User>(User.class));
	}
	/*class MyRowMapper implements RowMapper<User>{

		@Override
		public User mapRow(ResultSet rs, int rowNum) {
			// TODO Auto-generated method stub
			return null;
		}
//		
	}*/
}
